using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Security.Principal;

namespace Gothic_Starter
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            force_size_panel.Enabled = force_resolution_checkBox.Checked;
            advanced_panel.Enabled = advanced_settings_checkBox.Checked;
        }

        private void gothic_path_folderButton_Click(object sender, EventArgs e)
        {
            if (gothic_path_browserDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if (gothic_path_browserDialog.SelectedPath != "")
                    gothic_path_textBox.Text = gothic_path_browserDialog.SelectedPath;
            }
        }

        private void run_button_Click(object sender, EventArgs e)
        {
            string path = gothic_path_textBox.Text;

            // path check
            if (path == "")
            {
                MessageBox.Show("No path", "Wrong path", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (!File.Exists(path + "gothic.exe"))
            {
                MessageBox.Show("GOTHIC.EXE not detected", "Wrong path", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (!File.Exists(path + "gothic.ini"))
            {
                MessageBox.Show("Gothic.ini not detected", "Wrong path", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            // end of path check

            // check if there is need to change ini
            bool changeInit = false;
            if (force_resolution_checkBox.Checked) changeInit = true;
            if (windowed_checkBox.Checked) changeInit = true;
            if (force_32_checkBox.Checked) changeInit = true;

            bool isAdmin = new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator) ? true : false;

            if (!isAdmin)
            {
                MessageBox.Show("No admin rights.\nAdmin is required to change settings.\nSettings would be default.", "Admin required", MessageBoxButtons.OK, MessageBoxIcon.Information);
                changeInit = false;
            }

            if (changeInit)
            {
                // check if there is widht or heigth in textbox
                if(width_textBox.Text == "" || heigth_textBox.Text == ""){
                            MessageBox.Show("No width or heigth specified but need", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }

                string newinit = "";

                string[] init = File.ReadAllLines(path + "gothic.ini");
                foreach (string element in init)
                {
                    // add every commend and section
                    char[] temp = element.ToCharArray();
                    if (temp.Length == 0) continue;
                    if (temp[0] == ';' || temp[0] == '['){ 
                        newinit += element + "\n";
                        continue;
                    }

                    // add everything that have no =
                    string[] temps = element.Split('=');
                    if (temps.Length != 2){
                        newinit += element + "\n";
                        continue;
                    }

                    // change parameters

                    // resolution
                    if (force_resolution_checkBox.Checked)
                    {
                        if (temps[0] == "zVidResFullscreenX")
                        {
                            newinit += "zVidResFullscreenX=" + width_textBox.Text + "\n";
                            continue;
                        }
                        if (temps[0] == "zVidResFullscreenY")
                        {
                            newinit += "zVidResFullscreenY=" + heigth_textBox.Text + "\n";
                            continue;
                        }
                    }

                    // 32 bits color
                    if (force_32_checkBox.Checked)
                    {
                        if (temps[0] == "zVidResFullscreenBPP")
                        {
                            newinit += "zVidResFullscreenBPP=32\n";
                            continue;
                        }
                    }

                    // window mode
                    if (windowed_checkBox.Checked)
                    {
                        if (temps[0] == "zStartupWindowed")
                        {
                            newinit += "zStartupWindowed=1\n";
                            continue;
                        }
                    }

                    if (advanced_settings_checkBox.Checked)
                    {
                        // high details
                        if (high_detail_checkBox.Checked)
                        {
                            if (temps[0] == "modelDetail")
                            {
                                newinit += "modelDetail=1.0\n";
                                continue;
                            }
                        }

                        // blood details
                        // 0 = off, values from 1 to 3
                        if (temps[0] == "bloodDetail")
                        {
                            if (blood_1.Checked)
                            {
                                newinit += "bloodDetail=1\n";
                                continue;
                            }
                            if (blood_2.Checked)
                            {
                                newinit += "bloodDetail=2\n";
                                continue;
                            }
                            if (blood_3.Checked)
                            {
                                newinit += "bloodDetail=3\n";
                                continue;
                            }
                            if (blood_off.Checked)
                            {
                                newinit += "bloodDetail=0\n";
                                continue;
                            }
                        }

                        // disable mouse
                        if (disable_mouse_checkBox.Checked)
                        {
                            if (temps[0] == "enableMouse")
                            {
                                newinit += "enableMouse=0\n";
                                continue;
                            }
                        }

                        // more texture details
                        if (texture_details_checkBox.Checked)
                        {
                            if (temps[0] == "zDetailTexturesEnabled")
                            {
                                newinit += "zDetailTexturesEnabled=1\n";
                                continue;
                            }
                        }

                        // hidden menu
                        if (hidden_menu_checkBox.Checked)
                        {
                            if (temps[0] == "extendedMenu")
                            {
                                newinit += "extendedMenu=1";
                            }
                        }

                    }

                    // add any else lines to new file
                    newinit += element + "\n";
                }
                try
                {
                    //File.WriteAllText(path + @"gothic.ini", newinit);
                    StreamWriter sw = new StreamWriter(path + "Gothic.ini");
                    sw.WriteLine(newinit);
                    sw.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Save error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            
            try
            {
                System.Diagnostics.Process.Start(path + "gothic.exe");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Run program", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            force_size_panel.Enabled = force_resolution_checkBox.Checked;
        }

        private void advanced_settings_checkBox_CheckedChanged(object sender, EventArgs e)
        {
            advanced_panel.Enabled = advanced_settings_checkBox.Checked;
        }
    }
}

